package com.coursehub.course.service.impl;

import com.coursehub.course.entity.Course;
import com.coursehub.course.repository.CourseRepository;
import com.coursehub.course.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class CourseServiceImpl implements CourseService {
    
    @Autowired
    private CourseRepository courseRepository;
    
    @Override
    public Course createCourse(Course course) {
        return courseRepository.save(course);
    }
    
    @Override
    public Optional<Course> getCourseById(Long id) {
        return courseRepository.findById(id);
    }
    
    @Override
    public List<Course> getAllCourses() {
        return courseRepository.findAll();
    }
    
    @Override
    public Page<Course> getCoursesPaginated(Pageable pageable) {
        return courseRepository.findAll(pageable);
    }
    
    @Override
    public List<Course> getCoursesByTeacherId(Long teacherId) {
        return courseRepository.findByTeacherId(teacherId);
    }
    
    @Override
    public Page<Course> getCoursesByTeacherId(Long teacherId, Pageable pageable) {
        return courseRepository.findByTeacherId(teacherId, pageable);
    }
    
    @Override
    public List<Course> getPublishedCourses() {
        return courseRepository.findPublishedCourses();
    }
    
    @Override
    public Page<Course> getPublishedCourses(Pageable pageable) {
        return courseRepository.findPublishedCourses(pageable);
    }
    
    @Override
    public Course updateCourse(Long id, Course courseDetails) {
        Optional<Course> optionalCourse = courseRepository.findById(id);
        if (optionalCourse.isPresent()) {
            Course course = optionalCourse.get();
            course.setName(courseDetails.getName());
            course.setDescription(courseDetails.getDescription());
            course.setTeacherId(courseDetails.getTeacherId());
            course.setTeacherName(courseDetails.getTeacherName());
            course.setCourseCode(courseDetails.getCourseCode());
            course.setMaxStudents(courseDetails.getMaxStudents());
            course.setStatus(courseDetails.getStatus());
            course.setStartDate(courseDetails.getStartDate());
            course.setEndDate(courseDetails.getEndDate());
            return courseRepository.save(course);
        }
        return null;
    }
    
    @Override
    public void deleteCourse(Long id) {
        courseRepository.deleteById(id);
    }
    
    @Override
    public boolean existsByCourseCode(String courseCode) {
        return courseRepository.existsByCourseCode(courseCode);
    }
    
    @Override
    public Long countByTeacherId(Long teacherId) {
        return courseRepository.countByTeacherId(teacherId);
    }
    
    @Override
    public Long countPublishedCourses() {
        return courseRepository.countByStatus(Course.CourseStatus.PUBLISHED);
    }
}